//
//  ViewControllerForGongLue.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewControllerForGongLue.h"
#import "PCH.pch"
#import "ItemForGuoNei.h"
#import "ItemForGuoWai.h"
#import "ViewControllerOfClickGuoWai.h"
#import <UIImageView+WebCache.h>


#import <CommonCrypto/CommonDigest.h>
#import "Reachability.h"

@interface ViewControllerForGongLue () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UISegmentedControl *segment;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain)UIButton *buttonOfLeft;

@end

@implementation ViewControllerForGongLue

- (void)dealloc {
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [_buttonOfLeft release];
    [_flowLayout release];
    [_collectionView release];
    [_segment release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"passID" object:nil];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self panDuanWangLuo];
    
    self.navigationItem.title = @"旅行攻略";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil];
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    /** 主页分段控制条 */
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor whiteColor];
    view1.frame = CGRectMake(0, 64, WIDTH, 36);
    [self.view addSubview:view1];
    [view1 release];
    view1.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    /** 创建分栏 */
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"国外", @"国内"]];
    self.segment.frame = CGRectMake(5, 2, WIDTH - 10, 32);
    [view1 addSubview:self.segment];
    self.segment.selectedSegmentIndex = 0;
    self.segment.backgroundColor = [UIColor colorWithRed:230 / 256.f green:230 / 256.f blue:230 / 256.f alpha:0.5];
    [_segment release];
    self.segment.dk_tintColorPicker = DKColorWithColors([UIColor lightGrayColor], [UIColor whiteColor]);
    
    [self createCollectionView];
    
    /** KVO: 使UISegmentedControl 和 collectionView关联 */
    [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
    [self.segment addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
    
#pragma mark - 点击攻略 国外 国内 item, 推出下一界面
    [[NSNotificationCenter defaultCenter] addObserverForName:@"passID" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
       NSString *str = [note.userInfo objectForKey:@"ID"];
        
        ViewControllerOfClickGuoWai *vc = [[ViewControllerOfClickGuoWai alloc] init];
        vc.id = str;
        vc.name = [note.userInfo objectForKey:@"name"];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }];
    
}

#pragma mark - 判断网络状态
- (void)panDuanWangLuo {
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:{
            isExistenceNetwork = NO;
            NSLog(@"`````````网络不给力");
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前无网络" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            
            [alert addAction:actionOK];
            
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
            
            break;
        }
        case ReachableViaWiFi:{
            isExistenceNetwork = YES;
            break;
        }
        case ReachableViaWWAN:{
            isExistenceNetwork = YES;
            break;
        }
    }
    
}

#pragma mark - KVO主页: SegmentedControl关联collectionView
- (void)action:(UISegmentedControl *)seg {
    
    [UIView animateWithDuration:0.5 animations:^{
        switch (seg.selectedSegmentIndex) {
            case 0:
            {
                self.collectionView.contentOffset = CGPointZero;
                break;
            }
            case 1:
            {
                self.collectionView.contentOffset = CGPointMake(WIDTH, 0);
            }
            default:
                break;
        }
    }];
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    if ([[change objectForKey:@"new"] CGPointValue].x == WIDTH) {
        
        self.segment.selectedSegmentIndex = 1;
        
    } else if ([[change objectForKey:@"new"] CGPointValue].y == 0) {
        
        self.segment.selectedSegmentIndex = 0;
    }
}

#pragma mark - 创建collectionView
- (void)createCollectionView {
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 100 - 49);
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, HEIGHT - 100 - 49) collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [_collectionView release];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    self.collectionView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    [self.collectionView registerClass:[ItemForGuoWai class] forCellWithReuseIdentifier:@"pool1"];
    [self.collectionView registerClass:[ItemForGuoNei class] forCellWithReuseIdentifier:@"pool"];
    
    
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        ItemForGuoWai *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
        item.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor blackColor]);

        
        return item;
    } else {
        ItemForGuoNei *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
        item.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor blackColor]);
        
        return item;
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    [self.collectionView reloadData];
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
